<?php
/**
 * Javascript request handler
 *
 * ...
 */
require_once dirname(__FILE__) .'/cse-init.php';

// fetch required objects from Cinnamon registry
$db 	= Cinnamon::registry('db');
$params = Cinnamon::registry('params');
$events = Cinnamon::registry('events');
$config = Cinnamon::registry('config');

// validate the site id
$count = $db->getValue("SELECT COUNT(site_id) FROM $db->sites WHERE site_id='". $db->escape($params->site_id) ."'"); 
if ( $count != 1 ) {
	exit;
}
unset($count);

// let's start serving the javascript code

// headers (javascript content, always expired)
// taken from: http://www.php.net/manual/en/function.header.php#50810
header('Content-type: text/javascript');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

//
// PRE JAVASCRIPT EVENT
//

// before the Cinnamon code, preJavascript event occurs
$events->dispatchEvent( new Event('preJavascript', 'site_id', $params->site_id) );
?>
var Cinnamon = {
<?php
//
// JAVASCRIPT EVENT
//

Cinnamon::loadClass('JavascriptEvent');
$event = new JavascriptEvent('site_id', $params->site_id);
$event->addQuotedParameter('ip_address', $_SERVER['REMOTE_ADDR']);
$event->addParameter('request_url', 'document.location.href');
$event->addParameter('request_title', 'document.title');
$events->dispatchEvent($event);
?>	smell: function() {
		var trackURL = '<?php echo CSEURL; ?>/cse-track.php?site_id=<?php echo $params->site_id; ?>';
		
		var trackParams = Array();	
<?php /** print track parameters */ foreach ( $event->getAllParameters() as $name=>$value ): ?>
		trackURL = trackURL + '&' + encodeURIComponent('<?php echo $name; ?>') +'='+ encodeURIComponent(<?php echo $value; ?>); 
<?php endforeach; ?>
		this.makeRequest(trackURL, <?php if ( isset($params->debug) ): echo 'true'; else: echo 'false'; endif; ?>);
	},
	
	makeRequest: function(url, debug) {
		if ( debug ) {
			location.href = url;
		} else {
			var img = new Image(1, 1);
			img.src = url;
			img.onload = function() { return; };
		}
	},
	
	<? if 	(($config->exists('track.downloads.site.'.$params->site_id)) && 
			($config->get('track.downloads.site.'.$params->site_id) == 'y')) { ?>
	trackDownloads: function() {
	
		var a = document.getElementsByTagName('a');
		
		<?
			$arr =  $config->get('track.downloads.extensions.site.'.$params->site_id);
			foreach($arr as $item) $s .= $item."|";
			$s = "(".substr($s,0,-1).")";
			
		?>
		
		var re = /\b\.<?php echo $s; ?>$\b/;

		for (i=0; i<=a.length-1; i++) {
			if (re.test(a[i])) {
				newTrackURL = "<?php echo CSEURL; ?>/cse-track.php?site_id=<?php echo $params->site_id; ?>&request_type=download&request_url="+a[i];
				a[i].setAttribute('href',newTrackURL);
			}
		}
		
	}
	<? } // end if tracking downloads ?>
};
function onDOMReadySmell() {
	this.n = typeof this.n == 'undefined' ? 0 : this.n + 1;
	if ( typeof document.getElementsByTagName != 'undefined' 
		&& ( document.getElementsByTagName('body')[0] != null || document.body != null ) ) {	
	<? if 	(($config->exists('track.downloads.site.'.$params->site_id)) && 
			($config->get('track.downloads.site.'.$params->site_id) == 'y')) { ?>
		Cinnamon.trackDownloads();
	<? } // end if tracking downloads ?>
		Cinnamon.smell();
	} else if( this.n < 60 ) {
		setTimeout('onDOMReadySmell()', 250);
	}
}
onDOMReadySmell();
<?php 
//
// POST JAVASCRIPT EVENT
//

// at the end, the postJavascript event occurs
unset($event);
$events->dispatchEvent( new Event('postJavascript', 'site_id', $params->site_id) );
?>